package org.gcb.core.sandbox;

import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.InputEvent;
import java.io.IOException;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.TransferHandler;
import org.gcb.core.model.Armor;
import org.gcb.core.model.EArmorSlot;
import org.gcb.core.model.Item;
import org.openide.util.Exceptions;

/**
 *
 * @author Jason
 */
public class ItemToTransferSupport extends TransferHandler{
    private EArmorSlot armorSlot;

    public ItemToTransferSupport(EArmorSlot armorSlot) {
	this.armorSlot = armorSlot;
    }

    @Override
    public boolean canImport(TransferSupport support) {
	if (!support.isDrop()) {
	    return false;	    //only supporting drag and drop - no cut/copy/paste
	}
	if (!support.isDataFlavorSupported(Item.ITEM_DATA_FLAVOR)) {
	    return false;	    //only suporting org.gcb.core.model.Item transfers
	}
	Armor armor = null;
	try {
	    armor = Armor.class.cast(support.getTransferable().getTransferData(Item.ITEM_DATA_FLAVOR));
	} catch (UnsupportedFlavorException ex) {
	    Exceptions.printStackTrace(ex);
	} catch (IOException ex) {
	    Exceptions.printStackTrace(ex);
	}
	EArmorSlot slot = armor.getSlot();
	return 0==slot.compareTo(armorSlot);
//        return instance;
    }

    @Override
    @SuppressWarnings("unchecked")
    public boolean importData(JComponent comp, Transferable t) {
	JComponent component = comp;
	Transferable transferable = t;
	try {
	    ((JSlot) comp).getModel().setItem(Item.class.cast(t.getTransferData(Item.ITEM_DATA_FLAVOR)));

	} catch (UnsupportedFlavorException ex) {
	    Exceptions.printStackTrace(ex);
	} catch (IOException ex) {
	    Exceptions.printStackTrace(ex);
	}
	return true;
    }

    @Override
    protected Transferable createTransferable(JComponent c) {
	return super.createTransferable(c);
    }

    @Override
    public int getSourceActions(JComponent c) {
	return super.getSourceActions(c);
    }

    @Override
    public Icon getVisualRepresentation(Transferable t) {
	return super.getVisualRepresentation(t);
    }

    @Override
    public void exportAsDrag(JComponent comp, InputEvent e, int action) {
	super.exportAsDrag(comp, e, action);
    }

    @Override
    protected void exportDone(JComponent source, Transferable data, int action) {
	super.exportDone(source, data, action);
    }

}
